import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    id: root
    width: 814
    height: 700
    visible: true
    minimumHeight: height
    minimumWidth: width
    maximumHeight: height
    maximumWidth: width
    title: qsTr("广西玉林市幕光科技 modbus调试工具 v1.0")
    property var app: MGApp
    property int fontSize: 11
    property int titleWidth: 180
    property bool linkStatus: false
    property var rev1: RegularExpressionValidator {
        regularExpression: RegExp("[0-9A-Fa-fxXhH]+$")
    }
    property var rev2: RegularExpressionValidator {
        regularExpression: RegExp("[0-9A-Fa-f xXhH]+$")
    }

    Column {
        spacing: 8
        anchors.fill: parent
        anchors.margins: 10
        anchors.horizontalCenter: parent.horizontalCenter

        Row {
            spacing: 6
            Text {
                font.pointSize: fontSize
                text: qsTr("协议:")
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
            }

            ComboBox {
                id: mode_select
                model: ["RTU", "TCP","UDP"]
                font.pointSize: fontSize
                width: 58
                enabled: !app.linkStatus
                anchors.verticalCenter: parent.verticalCenter
                currentIndex: app.protocol
                onCurrentIndexChanged: {
                    app.protocol = currentIndex
                }
            }

            Text {
                font.pointSize: fontSize
                visible: mode_select.currentIndex === 0
                text: qsTr("串口:")
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
            }

            ComboBox {
                id: uart_select
                visible: mode_select.currentIndex === 0
                model: app.serialList
                font.pointSize: fontSize
                width: 65
                anchors.verticalCenter: parent.verticalCenter
                currentIndex: getIdx(app.device, app.serialList)
                onCurrentIndexChanged: {
                    if (app.protocol === 0){
                    if (currentIndex !== 0) {
                        app.device = model[currentIndex]
                        app.openSerial();
                    } else {
                        app.closeSerial()
                    }
                    }
                }
            }

            Text {
                visible: mode_select.currentIndex === 0
                font.pointSize: fontSize
                text: qsTr("波特率:")
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
            }

            ComboBox {
                visible: mode_select.currentIndex === 0
                id: baus_select
                model: [110, 300, 600, 4800, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 56000, 57600, 115200, 128000, 256000]
                currentIndex: getIdx(app.baud, model)
                width: 75
                anchors.verticalCenter: parent.verticalCenter
                font.pointSize: fontSize
                onCurrentIndexChanged: {
                    app.baud = model[currentIndex]
                }
            }
            Text {
                visible: mode_select.currentIndex === 0
                font.pointSize: fontSize
                text: qsTr("校验位:")
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
            }

            ComboBox {
                visible: mode_select.currentIndex === 0
                id: parity_select
                model: ['无校验', '偶校验', '奇校验','空格','标记']
                width: 75
                anchors.verticalCenter: parent.verticalCenter
                font.pointSize: fontSize
                property var map: [0, 1, 2, 3, 4]
                currentIndex: getIdx(app.parity, map)

                onCurrentIndexChanged: {
                    app.parity = map[currentIndex]
                }
            }
            Text {
                visible: mode_select.currentIndex === 0
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
                text: qsTr("数据位:")
            }
            ComboBox {
                visible: mode_select.currentIndex === 0
                id: dataBit_select
                model: [5, 6, 7, 8]
                width: 35
                anchors.verticalCenter: parent.verticalCenter
                currentIndex: getIdx(app.dataBit, model)
                font.pointSize: fontSize
                onCurrentIndexChanged: {
                    app.dataBit = model[currentIndex]
                }
            }

            Text {
                visible: mode_select.currentIndex === 0
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
                text: qsTr("停止位:")
            }
            ComboBox {
                visible: mode_select.currentIndex === 0
                id: stopBit_select
                model: ["1", "2", "1.5"]
                width: 40
                property var map: [1,2,3]
                anchors.verticalCenter: parent.verticalCenter
                currentIndex: getIdx(app.stopBit, map)
                font.pointSize: fontSize
                onCurrentIndexChanged: {
                    app.stopBit = map[currentIndex]
                }
            }

            Button {
                visible: mode_select.currentIndex === 0
                id: uart_open_btn
                text: !app.linkStatus ? "打开" : "关闭"
                anchors.verticalCenter: parent.verticalCenter
                font.pointSize: fontSize
                height: 35
                width: 80

                onClicked: {
                        if (!app.linkStatus) {
                            app.openSerial()
                        } else {
                            app.closeSerial()
                        }
                }
            }

            Rectangle {
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                height: 1
                width: 43
                color: "#00000000"
            }

            Text {
                font.pointSize: fontSize
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                text: qsTr("IP地址:")
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
            }

            TextField {
                width: 150
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                enabled: !app.linkStatus
                validator: RegularExpressionValidator {
                    regularExpression: RegExp("[0-9.]+$")
                }
                text : app.ip
                onTextChanged: {
                    app.ip = text
                }
            }

            Text {
                font.pointSize: fontSize
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                text: qsTr("端口:")
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
            }

            TextField {
                width: 80
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                enabled: !app.linkStatus
                text: app.port
                validator: RegularExpressionValidator {
                    regularExpression: RegExp("[0-9]+$")
                }
                onTextChanged: {
                    app.port = Number(text)
                }
            }

            Text {
                font.pointSize: fontSize
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                text: qsTr("MBAP ID:")
                horizontalAlignment: Text.AlignRight
                anchors.verticalCenter: parent.verticalCenter
            }

            TextField {
                width: 80
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                text: app.mbapId
                validator: RegularExpressionValidator {
                    regularExpression: RegExp("[0-9]+$")
                }
                onTextChanged: {
                    app.mbapId = Number(text)
                }
            }

            Rectangle {
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                height: 1
                width: 43
                color: "#00000000"
            }

            Button {
                visible: mode_select.currentIndex === 1 || mode_select.currentIndex === 2
                id: tcp_link_btn
                text: !app.linkStatus ? "连接" : "断开"
                anchors.verticalCenter: parent.verticalCenter
                font.pointSize: fontSize
                height: 35
                width: 80

                onClicked: {
                    if (!app.linkStatus) {
                        app.openSerial()
                    } else {
                        app.closeSerial()
                    }
                }
            }
        }

        Rectangle {
            width: parent.width
            height: 1
            color: "#CCCCCC"
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("读多个线圈(0x01)：")
                font.pointSize: fontSize
                width: titleWidth
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: qsTr("485地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: slaveText1
                width: 50
                text: ""
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: regText1
                width: 50
                text: ""
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器个数")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: lenText1
                width: 50
                text: ""
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器值")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: valText1
                width: 100
                text: ""
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev2
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Button {
                text: "读取"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60

                onClicked: {
                    valText1.text = ""
                    app.sendData(0x01, slaveText1.text, regText1.text,
                                 lenText1.text, valText1.text)
                }
            }
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("读多个离散输入(0x02)：")
                font.pointSize: fontSize
                width: titleWidth
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: qsTr("485地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: slaveText2
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: regText2
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器个数")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: lenText2
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器值")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: valText2
                width: 100
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev2
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Button {
                text: "读取"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60
                onClicked: {
                    valText2.text = ""
                    app.sendData(0x02, slaveText2.text, regText2.text,
                                 lenText2.text, valText2.text)
                }
            }
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("读多个保持寄存器(0x03)：")
                font.pointSize: fontSize
                width: titleWidth
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: qsTr("485地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: slaveText3
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: regText3
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器个数")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: lenText3
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器值")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: valText3
                width: 100
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev2
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Button {
                text: "读取"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60
                onClicked: {
                    valText3.text = ""
                    app.sendData(0x03, slaveText3.text, regText3.text,
                                 lenText3.text, valText3.text)
                }
            }
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("读多个输入寄存器(0x04)：")
                font.pointSize: fontSize
                width: titleWidth
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: qsTr("485地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: slaveText4
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: regText4
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器个数")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: lenText4
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器值")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: valText4
                width: 100
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev2
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Button {
                text: "读取"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60
                onClicked: {
                    valText4.text = ""
                    app.sendData(0x04, slaveText4.text, regText4.text,
                                 lenText4.text, valText4.text)
                }
            }
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("写单个线圈(0x05)：")
                width: titleWidth
                font.pointSize: fontSize
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: qsTr("485地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: slaveText5
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: regText5
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器个数")
                font.pointSize: fontSize
                enabled: false
                color: "#00000000"
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: lenText5
                width: 50
                font.pointSize: fontSize
                enabled: false

                background: Rectangle {
                    border.color: "#00000000"
                    color: "#00000000"
                }

                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器值")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: valText5
                width: 100
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Button {
                text: "写入"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60

                onClicked: {
                    app.sendData(0x05, slaveText5.text, regText5.text,
                                 lenText5.text, valText5.text)
                }
            }
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("写单个保持寄存器(0x06)：")
                font.pointSize: fontSize
                width: titleWidth
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: qsTr("485地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: slaveText6
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: regText6
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }

            Text {
                text: qsTr("寄存器个数")
                font.pointSize: fontSize
                enabled: false
                color: "#00000000"
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            TextField {
                id: lenText6
                width: 50
                font.pointSize: fontSize
                enabled: false
                background: Rectangle {
                    border.color: "#00000000"
                    color: "#00000000"
                }
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }

            Text {
                text: qsTr("寄存器值")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: valText6
                width: 100
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Button {
                text: "写入"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60

                onClicked: {
                    app.sendData(0x06, slaveText6.text, regText6.text,
                                 lenText6.text, valText6.text)
                }
            }
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("写多个线圈(0x0F)：")
                font.pointSize: fontSize
                width: titleWidth
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: qsTr("485地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: slaveTextF
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: regTextF
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器个数")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: lenTextF
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev2
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器值")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: valTextF
                width: 100
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev2
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Button {
                text: "写入"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60
                onClicked: {
                    app.sendData(0x0F, slaveTextF.text, regTextF.text,
                                 lenTextF.text, valTextF.text)
                }
            }
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("写多个保持寄存器(0x10)：")
                width: titleWidth
                font.pointSize: fontSize
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            Text {
                text: qsTr("485地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: slaveText10
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器地址")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: regText10
                width: 50
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev1
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Text {
                text: qsTr("寄存器值")
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }
            TextField {
                id: valText10
                width: 233
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev2
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }
            Button {
                text: "写入"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60
                onClicked: {
                    app.sendData(0x10, slaveText10.text, regText10.text, "",
                                 valText10.text)
                }
            }
        }

        Column {
            spacing: 8
            Row {
                spacing: 4
                Text {
                    text: qsTr("读写多个保持寄存器(0x09):")
                    width: titleWidth
                    font.pointSize: fontSize
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                }

                Text {
                    text: qsTr("485地址")
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                }
                TextField {
                    id: slaveText9
                    width: 50
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                    selectByMouse: true
                    validator: rev1
                    onTextChanged: {
                        text = app.toUpper(text)
                    }
                }
                Text {
                    text: qsTr("读寄存器地址")
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                }
                TextField {
                    id: regTextr9
                    width: 50
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                    selectByMouse: true
                    validator: rev1
                    onTextChanged: {
                        text = app.toUpper(text)
                    }
                }
                Text {
                    text: qsTr("读寄存器个数")
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                }
                TextField {
                    id: lenTextr9
                    width: 50
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                    selectByMouse: true
                    validator: rev1
                    onTextChanged: {
                        text = app.toUpper(text)
                    }
                }
                Text {
                    text: qsTr("读寄存器值")
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                }
                TextField {
                    id: valTextr9
                    width: 100
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                    selectByMouse: true
                    validator: rev2
                    onTextChanged: {
                        text = app.toUpper(text)
                    }
                }
            }

            Row {
                spacing: 4
                Rectangle {
                    height: 1
                    width: titleWidth + 70 //- 33
                    color: "#00000000"
                }

                Text {
                    text: qsTr("写寄存器地址")
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                }
                TextField {
                    id: regTextw9
                    width: 50
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                    selectByMouse: true
                    validator: rev1
                    onTextChanged: {
                        text = app.toUpper(text)
                    }
                }
                Text {
                    text: qsTr("写寄存器值")
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                }
                TextField {
                    id: valTextw9
                    width: 250
                    font.pointSize: fontSize
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    anchors.verticalCenter: parent.verticalCenter
                    selectByMouse: true
                    validator: rev2
                    onTextChanged: {
                        text = app.toUpper(text)
                    }
                }
                //                Rectangle{
                //                    height: 1
                //                    width: 100
                //                    color: "#00000000"
                //                }
                Button {
                    text: "读写"
                    font.pointSize: fontSize
                    anchors.verticalCenter: parent.verticalCenter
                    height: 30
                    width: 60
                    onClicked: {
                        valTextr9.text = ""
                        app.sendData(0x09, slaveText9.text, regTextr9.text,
                                     lenTextr9.text, valTextw9.text,
                                     regTextw9.text)
                    }
                }
            }
        }

        Row {
            spacing: 4
            Text {
                text: qsTr("自定义发送")
                width: titleWidth + 60
                font.pointSize: fontSize
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
            }

            TextField {
                id: valText0
                width: 392
                font.pointSize: fontSize
                horizontalAlignment: Text.AlignRight
                verticalAlignment: Text.AlignVCenter
                anchors.verticalCenter: parent.verticalCenter
                selectByMouse: true
                validator: rev2
                onTextChanged: {
                    text = app.toUpper(text)
                }
            }

            Button {
                text: "发送"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 60
                onClicked: {
                    app.sendData(0x00, "", "", "", valText0.text)
                }
            }

            Button {
                text: "CRC发送"
                font.pointSize: fontSize
                anchors.verticalCenter: parent.verticalCenter
                height: 30
                width: 90
                onClicked: {
                    app.sendData(0x00, "", "", "", valText0.text, "1")
                }
            }
        }

        Rectangle {
            width: parent.width
            height: 1
            color: "#CCCCCC"
        }

        Row {
            spacing: 4
            width: parent.width
            Column {
                spacing: 4
                width: parent.width / 2 - 2
                Row {
                    spacing: 4
                    Label {
                        text: "数据发送区"
                        width: 310
                        font.pointSize: fontSize
                        verticalAlignment: Text.AlignVCenter
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    Button {
                        text: "清空数据"
                        font.pointSize: fontSize
                        anchors.verticalCenter: parent.verticalCenter
                        onClicked: {
                            sendModel.clear()
                        }
                    }
                }
                Rectangle {
                    id: sendrect
                    width: parent.width
                    height: 160
                    border.color: 'gray'
                    clip: true

                    ListView {
                        id: sendDataShow
                        model: sendModel
                        anchors.fill: sendrect
                        anchors.margins: 5
                        delegate: TextEdit {
                            text: modelData
                            selectByMouse: true
                            font.pointSize: 10
                            height: 20
                        }
                    }
                }
            }

            Column {
                spacing: 4
                width: parent.width / 2 - 2
                Row {
                    spacing: 4
                    Label {
                        text: "数据接收区:"
                        width: 310
                        font.pointSize: fontSize
                        verticalAlignment: Text.AlignVCenter
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    Button {
                        text: "清空数据"
                        font.pointSize: fontSize
                        anchors.verticalCenter: parent.verticalCenter
                        onClicked: {
                            recvModel.clear()
                        }
                    }
                }
                Rectangle {
                    width: parent.width
                    height: 160
                    border.color: 'gray'
                    clip: true
                    ListView {
                        id: recvDataShow
                        anchors.margins: 5
                        model: recvModel
                        anchors.fill: parent
                        delegate: TextEdit {
                            text: modelData
                            selectByMouse: true
                            font.pointSize: 10
                            height: 20
                        }
                    }
                }
            }
        }
        Row {
            Text {
                width: 150
                text: qsTr("响应时间:") + (app.responseTime / 1000) + "ms"
            }

            Text {
                width: 100
                text: qsTr("发送帧:") + app.sendframeNum
            }
            Text {
                width: 100
                text: qsTr("接收帧:") + app.recvframeNum
            }
            Text {
                width: 100
                text: qsTr("发送字节:") + app.sendByteNum
            }
            Text {
                width: 100
                text: qsTr("接收字节:") + app.recvByteNum
            }
        }
    }

    ListModel {
        id: sendModel
    }

    ListModel {
        id: recvModel
    }

    Connections {
        target: app
        onSendDataChanged: str => {
                               sendModel.append({
                                                    "modelData": str + "\r\n"
                                                })
                               if (sendModel.count > 1024) {
                                   sendModel.remove(0)
                               }

                               sendDataShow.currentIndex = sendModel.count - 1
                           }
        onRecvDataChanged: str => {
                               recvModel.append({
                                                    "modelData": str + "\r\n"
                                                })
                               if (recvModel.count > 1024) {
                                   recvModel.remove(0)
                               }

                               recvDataShow.currentIndex = recvModel.count - 1
                           }

        onRecvData: (code, valStr) => {
                        switch (code) {
                            case 0x01:
                            valText1.text = valStr
                            break
                            case 0x02:
                            valText2.text = valStr
                            break
                            case 0x03:
                            valText3.text = valStr
                            break
                            case 0x04:
                            valText4.text = valStr
                            break
                            case 0x09:
                            valTextr9.text = valStr
                            break
                        }
                    }
    }

    function getIdx(k, list) {
        for (var x in list) {
            if (k === list[x]) {
                return x
            }
        }
        return 0
    }
}
